<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
      xml:lang="en" lang="en">

<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link type="text/css" rel="stylesheet" href="../static/layui/css/layui.css" th:href="@{/layui.css}" media="all">
</head>
<body>
    <table class="layui-hide" id="test" lay-filter="test"></table>

    <script type="text/javascript" src="../static/layui/js/jquery-3.5.1.min.js" th:src="@{/jquery-3.5.1.min.js}"></script>
    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
            <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
            <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
        </div>
    </script>
    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>

    <script type="text/javascript" src="../static/layui/js/layui.js" th:src="@{/layui.js}" charset="utf-8"></script>

    <!--插入图片-->
    <script type="text/html" id="productImg">
        <img src="{{d.fileName}}" alt="">
    </script>

    <script type="text/javascript" th:inline="none">
        layui.use('table', function(){
            var table = layui.table;

            table.render({
                elem: '#test'
                ,url:'/list'
                ,toolbar: '#toolbarDemo'
                ,title: '用户数据表'
                ,totalRow: true
                ,cols: [[
                    {type: 'checkbox', fixed: 'left'}  // 复选框
                    ,{field:'id', title:'ID', width:100, fixed: 'left', unresize: true, sort: true, totalRowText: '合计'}
                    ,{field:'name', title:'商品名称', width:150}
                    ,{field:'description', title:'商品描述', width:180}
                    ,{field:'price', title:'商品价格', width:120, sort: true, totalRow: true}
                    ,{field:'stock', title:'商品库存', width:120, sort: true, totalRow: true}
                    ,{field:'categorylevelone', title:'一级分类', width:150}
                    ,{field:'categoryleveltwo', title:'二级分类', width:150}
                    ,{field:'categorylevelthree', title:'三级分类', width:150}
                    ,{field:'fileName', title:'商品图片', width:100, templet: '#productImg'}
                    ,{fixed: 'right', title:'操作', toolbar: '#barDemo'}
                ]]
                ,page: true
            });

            //工具栏事件
            table.on('toolbar(test)', function(obj){
                var checkStatus = table.checkStatus(obj.config.id);
                switch(obj.event){
                    case 'getCheckData':
                        var data = checkStatus.data;
                        layer.alert(JSON.stringify(data));
                        break;
                    case 'getCheckLength':
                        var data = checkStatus.data;
                        layer.msg('选中了：'+ data.length + ' 个');
                        break;
                    case 'isAll':
                        layer.msg(checkStatus.isAll ? '全选': '未全选');
                        break;
                }
            });

            //监听工具条  跟lay-filter属性绑定
            table.on('tool(test)', function(obj){
                var data = obj.data;
                if(obj.event === 'del'){
                    layer.confirm('真的要删除' + data.name + '么?', function(index){
                        delUsr(data, obj, index);
                    });
                } else if(obj.event === 'edit'){
                    layer.prompt({
                        formType: 2
                        ,title: '修改名称为 ['+ data.name +'] 的价格吗'
                        ,value: data.price
                    }, function (value, index) {
                        editPrice(data, value, index, obj);

                    });
                }
            });

            function delUsr(data, obj, index) {
                $.ajax({
                    url: "/delete/" + data.id,
                    type: "GET",
                    success: function () {
                        layer.close(index);
                        obj.del();
                        layer.msg("删除成功", {icon: 6});
                    }
                });
            }
            
            function editPrice(data, value, index, obj) {  // 不需要加dataType，dataType是后台传来的数据
                $.ajax({
                    url: "/edit",
                    type: "POST",
                    data: JSON.stringify({"id": data.id, "price": value}),  // important
                    contentType: "application/json;charset=UTF-8",
                    success: function () {
                        layer.close(index);
                        obj.update({
                            price: value
                        });
                        layer.msg("编辑成功", {icon: 6});
                    }
                })
            }
        });
    </script>

</body>
</html>